package com.shenhaofeng.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.shenhaofeng.app.R;


/**
 * 带删除功能的EditText
 * Created by Administrator on 2016/7/18.
 */
public class DeleteEditText extends AppCompatEditText {


    private Drawable mDeleteDrawable;

    private boolean mDeleteEnable;





    public DeleteEditText(Context context) {
        this(context, null);
    }





    public DeleteEditText(Context context, AttributeSet attrs) {
        this(context, attrs, android.support.v7.appcompat.R.attr.editTextStyle);
    }





    public DeleteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DeleteEditText, defStyleAttr, 0);
        mDeleteDrawable = ta.getDrawable(R.styleable.DeleteEditText_deleteIcon);
        mDeleteEnable = ta.getBoolean(R.styleable.DeleteEditText_deleteEnable, true);
        ta.recycle();
        setText(getText());
    }





    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {

        if (hasDeleteEnable()) {
            if (!TextUtils.isEmpty(text)) {
                Drawable[] compoundDrawables = getCompoundDrawables();
                if (compoundDrawables[0] != null) {
                    mDeleteDrawable.setBounds(0, 0, compoundDrawables[0].getIntrinsicHeight(), compoundDrawables[0].getIntrinsicHeight());
                } else {
                    mDeleteDrawable.setBounds(0, 0, mDeleteDrawable.getIntrinsicHeight(), mDeleteDrawable.getIntrinsicHeight());
                }
                setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], mDeleteDrawable, compoundDrawables[3]);
            } else {
                Drawable[] compoundDrawables = getCompoundDrawables();
                setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], null, compoundDrawables[3]);
            }
        } else if (mDeleteDrawable != null && !mDeleteEnable) {
            Drawable[] compoundDrawables = getCompoundDrawables();
            setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], null, compoundDrawables[3]);
        }
    }


    /**
     * 设置删除按钮的显示状态
     * @param show
     */
    public void showDelete(boolean show) {
        mDeleteEnable = show;
        invalidate();
    }





    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (hasDeleteEnable() && !TextUtils.isEmpty(getText()) && event.getAction() == MotionEvent.ACTION_DOWN) {
            int xMax = getWidth() - getPaddingRight();
            int xMin = xMax - mDeleteDrawable.getBounds().width();
            int yMax = getHeight() / 2 + mDeleteDrawable.getBounds().height();
            int yMin = getHeight() / 2 - mDeleteDrawable.getBounds().height();
            float x = event.getX();
            float y = event.getY();
            if (x > xMin && x < xMax && y > yMin && y < yMax) {
                setText(null);
                return true;
            }
        }
        return super.onTouchEvent(event);
    }


    /**
     * 删除按钮是否有效
     * @return
     */
    protected boolean hasDeleteEnable() {
        return mDeleteEnable && mDeleteDrawable != null;
    }
}
